package com.example.webpractice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.webpractice.dto.KechengfenleiDTO;
import com.example.webpractice.po.KechengfenleiPO;
import com.example.webpractice.service.KechengfenleiService;
import com.example.webpractice.utils.ServerResponseEntity;
import com.example.webpractice.vo.KechengfenleiVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

import static com.example.webpractice.utils.ResponseEnum.kechengExsit;


/**
 * 课程分类
 * 后端接口
 */
@RestController
@RequestMapping("/kechengfenlei")
public class KechengfenleiController {
    @Autowired
    private KechengfenleiService kechengfenleiService;

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public ServerResponseEntity page(@RequestParam(name = "page") Integer page,
                  @RequestParam(name = "limit") Integer limit,
                  @RequestParam(name = "kechengfenlei", required = false) String kechengfenlei){
        QueryWrapper<KechengfenleiPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(kechengfenlei)) {
            queryWrapper.like("kechengfenlei", kechengfenlei);
        }
        return ServerResponseEntity.success(kechengfenleiService.queryPage(page, limit, queryWrapper));
    }

    /**
     * 详情
     */
    @RequestMapping("/info/{id}")
    public ServerResponseEntity info(@PathVariable("id") Long id) {
        KechengfenleiVO kechengfenleiVO = kechengfenleiService.selectById(id);
        return ServerResponseEntity.success(kechengfenleiVO);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public ServerResponseEntity save(@RequestBody KechengfenleiDTO kechengfenleiDto) {
        if (kechengfenleiService.selectKechengfenlei(kechengfenleiDto.getKechengfenlei()) != null) {
            return ServerResponseEntity.fail(kechengExsit);
        }
        kechengfenleiService.create(kechengfenleiDto);
        return ServerResponseEntity.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public ServerResponseEntity update(@RequestBody KechengfenleiDTO kechengfenleiDto) {
        if (kechengfenleiService.update(kechengfenleiDto) == 1) {
            return ServerResponseEntity.success();
        } else {
            return ServerResponseEntity.fail(-1, "更新失败");
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody Long[] ids) {
        if (kechengfenleiService.deleteBatchIds(Arrays.asList(ids)) != ids.length) {
            return ServerResponseEntity.fail(-1, "删除失败");
        }
        return ServerResponseEntity.success();
    }



    /**
     * 列表
     */
    @RequestMapping("/option")
    public ServerResponseEntity option() {
        List<String> list =  kechengfenleiService.getOption();
        return ServerResponseEntity.success(list);
    }
}
